package robbie.WeatherApp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.app.*;
import java.util.*;


public class OraseAdapter extends BaseAdapter 
{
	private Activity context;
	ArrayList<Oras> orase;
	
	
	public OraseAdapter(Activity unContext)
	{
		this.context = unContext;
		orase = new ArrayList<Oras>();
	}

	
	public int getCount() 
	{
		return orase.size();
	}


	
	public Oras getItem(int position) 
	{
		return orase.get(position);
	}


	public long getItemId(int arg0) 
	{
		// fiecare element sin lista poate avea un id dar nu e obligatoriu --> vezi daca ii bagi id mai incolo  !!!
		return 0;
	}


	public View getView(int position, View view, ViewGroup list)	
	{
		if(view==null){
		LayoutInflater inflater = context.getLayoutInflater();
		view = inflater.inflate(R.layout.adapter, null);
			}
		TextView numeOras = (TextView)view.findViewById(R.id.AdapterNumeOras);
		if (orase.get(position).nume.contains(" "))
		{
			String[] s = orase.get(position).nume.split(" ");
			String ss="";
			for(int i =0;i<s.length;i++)
				 ss = ss+s[i].substring(0,1).toUpperCase()+s[i].substring(1, s[i].length())+" "; 
			numeOras.setText(ss);
		}
		else
			numeOras.setText(orase.get(position).nume.substring(0, 1).toUpperCase()+orase.get(position).nume.substring(1, orase.get(position).nume.length()));
		
		return view;
	}
	
	public void adaugaOras(String numeOras)		// si mai urmeaza si alte campuir; executat in AddActivity
	{
		Oras o = new Oras();
				
		o.nume = numeOras.toLowerCase();
		boolean h = true;
		for(int i =0; i<orase.size();i++)
			if (orase.get(i).nume.compareTo(o.nume)==0) h = false;
		
		if(h)
		{
			if(orase.size()==0) orase.add(0,o);
			else
			{
				int i=0;
				while(  (i<orase.size()) && (orase.get(i).nume.compareTo(o.nume)<0))
					i++;
				orase.add(i, o);
			}
		}
		this.notifyDataSetChanged();
		
	}
	
	public void stergeOras(Oras o)
	{
		orase.remove(o);
		this.notifyDataSetChanged();
		
	}
	
	
	
}
